// zh_cjdianc/shopList/reserve.js
var a = getApp(), util = require("../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    date: '2018-09-30',
    time: '12:00',
    array: [],
    type:'',
    mobile:'',
    name:'',
    peoplenum:'',
    desc:'',
    index: 0,
    index2:0,
    people:['1人','2人','3人','4人','5人','6人及以上'],
    price:[],
    popErrorMsg:'',
    order_id:'',
  },
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var a = this.data.array;
    console.log(a);
    this.setData({
      index2: e.detail.value,
      type: a[e.detail.value]['id']
    })
  },
  bindPickerChange_people: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var a= 1,b=Number(e.detail.value),c;
    c = a + b;
    this.setData({
      peoplenum: c,
      index:e.detail.value
    })
  },
  tapdesc:function(e){
    console.log(e)
    this.setData({
      desc:e.detail.value
    })
  },
  tapmobile: function (e) {
    console.log(e)
    this.setData({
      mobile: e.detail.value
    })
  },
  tapname: function (e) {
    console.log(e)
    this.setData({
      name: e.detail.value
    })
  },
  goPaySuccess: function (e) {
    console.log(wx.getStorageSync('users'));
    var user_id = wx.getStorageSync('users').id;
    var n = this;
    var store_id = n.data.price['id'];
    var yd_date = n.data.date;
    var yd_time = n.data.time;
    var table_id = n.data.type;
    var name = n.data.name;
    var mobile = n.data.mobile;
    var people = n.data.peoplenum;
    var price = n.data.price['yd_price'];
    var desc = n.data.desc;
    var order_id = n.data.order_id;
    if (!table_id) {
      n.setData(
        { popErrorMsg: "该商家位置已满" }
      )
      n.ohShitfadeOut();return;
    }
    if(!mobile || !name){
      n.setData(
        { popErrorMsg: "请输入联系人或电话" }
      )
      n.ohShitfadeOut(); return;
    }
    if (!(/^1[34578]\d{9}$/.test(mobile))) {
      n.setData(
        { popErrorMsg: "请输入正确的手机号码" }
      )
      n.ohShitfadeOut(); return;
    }
    if (name.length > 20) {
      n.setData(
        { popErrorMsg: "请输入正确的用户名称" }
      )
    }  
    a.util.request({
      url: "entry/wxapp/newuserorderupdatesub",
      cachetime: "0",
      data: { store_id: store_id, yd_date: yd_date, yd_time: yd_time, table_id: table_id, name: name, mobile: mobile, people: people, price: price, desc: desc, user_id: user_id,order_id:order_id },
      success: function (t) {
        console.log(t.data)
        if(t.data.code == 0){
          wx.showModal({ title: '提示', 
          content: '修改成功',
           success: function (res) { 
             if (res.confirm) { 
               wx.navigateTo({
                 url: 'changeOrder',
                 success: function (res) {
                   wx.hideLoading() //隐藏加载框
                 },
                 fail: function (res) { },
                 complete: function (res) { },
               })
             } 
             else { 
               console.log('用户点击取消')
                } 
            }
          }) 
        }else{
          n.setData(
            { popErrorMsg: t.data.message }
          )
          n.ohShitfadeOut(); return;
        }
      }
    });
  },
  ohShitfadeOut() {
    var fadeOutTimeout = setTimeout(() => {
      this.setData({ popErrorMsg: '' });
      clearTimeout(fadeOutTimeout);
    }, 3000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    var n = this;
    a.setNavigationBarColor(this);
    var user_id = wx.getStorageSync('users').id;
    var order_id = options.order_id;
    a.util.request({
      url: "entry/wxapp/newuserorderupdate",
      cachetime: "0",
      data: { order_id: order_id, user_id: user_id },
      success: function (t) {
        if (t.data.data.table.length == 0){
          n.setData(
            { popErrorMsg: "该商家位置已满" }
          )
          n.ohShitfadeOut();return;
        }
        console.log(t.data)
        var b = t.data.data.order;
        n.setData({
          array:t.data.data.table,
          price:{
            id: b.store_id,
            yd_price: b.yd_price
          },
          type: b.tabletype_id,
          peoplenum: b.yd_people,
          date: b.date,
          time: b.time,
          name: b.yd_name,
          mobile: b.yd_mobile,
          order_id:b.id,
          desc:b.desc,
          index:b.yd_people-1,
          index2:b.index2,
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})